import http from '@/axios'
import {handleError} from "vue";
import {handleSuccess} from "@/api/index.js";

/**
 * 用户相关API
 */
export const userAPI = {
    //获取用户列表
    list(p){
        return http.get('/api/user/list', {params:{...p}})
            .then(res => handleSuccess( res, '获取用户列表成功'))
            .catch(error  =>handleError(error, '获取用户列表失败','服务器异常'));
    },
    // 创建用户
    createUser: (data) => http.post('/api/user/create', data),

    // 更新用户
    updateUser: (data) => http.put(`/api/user/update`, data),

    // 删除用户
    deleteUser: (id) => http.delete(`/api/user/delete/${id}`),

    // 恢复用户
    restoreUser: (id) => http.post(`/api/user/restore/${id}`),

    // 获取用户详情
    getUserById: (id) => http.get(`/api/user/get/${id}`),

    // 修改密码
    changePassword: (data) => http.put('/api/user/password', data)
        .then(res => handleSuccess(res, '密码修改成功'))
        .catch(error => handleError(error, '密码修改失败')),
    //修改mobile
    resetPhone: (data) => http.put('/api/user/mobile', data)
        .then(res => handleSuccess(res, '手机号修改成功'))
        .catch(error => handleError(error, '手机号修改失败')),

    // 用户注册相关API（保留原有注册接口）
    registerPublic: (data) => http.post('/api/pub/register', data),

    registerLawyer: (data) => http.post('/api/lawyer/register', data),

    // 发送验证码
    sendCode: (data) => http.post('/api/sms/send', data),

    // 用户登录
    loginByPassword: (data) => http.post('/api/auth/login', data),
    // 退出登录
    logout: (id) => http.post('/api/auth/logout', null, {
        params: { id }  // 注意参数名大写
    }),
    // 获取当前用户信息
    getCurrentUser: () => http.get('/api/user/authInfo'),

    //获取当前用户个人信息
    getUserPersionalInfo: () => http.get('/api/user/profile'),
    // 更新用户个人信息
    updateUserProfile: (data) => http.put('/api/user/updateProfile', data)
        .then(res => handleSuccess(res, '个人信息更新成功'))
        .catch(error => handleError(error, '个人信息更新失败')),

    // 实名认证接口
    submitAuth: (data) => http.post('/api/user/auth-submit', data, {
        params: { userId: data.userId }
    })
        .then(res => handleSuccess(res, '实名认证提交成功'))
        .catch(error => handleError(error, '实名认证提交失败')),

    getAuthStatus: (params) => http.get('/api/user/auth-status', { params })
        .then(res => handleSuccess(res, '获取认证状态成功'))
        .catch(error => handleError(error, '获取认证状态失败')),
}

// 保留原有导出
export default userAPI